<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class VehicleController extends Controller
{
	public function getIndex(Request $request)
	{		
			if($request['name']){
				$cinfo = DB::table('vehicle')
				 ->where('CarNum','like','%'.$request['name'].'%')
				->paginate(10);
			}else{
				$cinfo = DB::table('vehicle')
				->paginate(3);
			}	
			 $info = $request -> all();
			return view('Lzk.Vehicle.Vehicle',['cinfo'=>$cinfo,'info'=>$info]);	
	}	

	//解析添加
	public function getAdd()
	{
		return view('Lzk.Vehicle.AddVehicle');
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->only('CarClass','CarNum');
		$ins['State'] = 1;
		$ind = DB::table('vehicle')->insert($ins);
		if($ind){
				return redirect('/Vehicle');
			}else{
				return back();
			}
	}

	//执行删除
	public function getDel($id)
	{
		$carid = DB::table('gocar')->where('CarNumber',$id)->get();
		if($carid){
			echo '<script>alert("此车辆有预约不能删除");window.location.href="/Vehicle";</script>';
			die;
		}
		$del = DB::table('vehicle')->where('id',$id)->delete($id);
		if($del){
				return redirect('/Vehicle');
			}else{
				return back();
			}
	}
}
